const StorageManager = require('../util/storage_manager.js');

class BaseModel {
    constructor(id) {
        this.id = id; // 统一的主键
    }

    // 过滤存储时的字段
    toJSON() {
        const data = { ...this };
        delete data.store; // 忽略 `store`
        return data;
    }

    // 📌 通用方法：打印当前对象
    print() {
        console.log(this);
    }

    // 使用静态变量来缓存 `store` 实例
    static _storeInstance = null;

    // 静态 getter，用于获取 `store`
    static get store() {
        if (!this._storeInstance) {
            this._storeInstance = new StorageManager(this.name);
        }
        return this._storeInstance;
    }

    // 静态方法：获取所有数据
    static getAll() {
        return this.store.getAll();
    }

    // 静态方法：根据 ID 获取数据
    static get(id) {
        return this.store.get(id);
    }

    // 静态方法：保存数据
    static set(data) {
        return this.store.set(data);
    }

    // 静态方法：删除数据
    static delete(id) {
        return this.store.delete(id);
    }

    // 静态方法：清空数据
    static clear() {
        return this.store.clearAll();
    }
}

// **定义地址模型**
class AddressModel extends BaseModel {
    constructor(id, address, signalType, flag,changeValue) {
        super(id); // 唯一主键
        this.address = address;
        this.signalType = signalType;
        this.flag = flag;
        this.changeValue = changeValue;
    }
}

// **定义频道模型**
class ChannelModel extends BaseModel {
    constructor({ id, name, color, weight, minWeight, maxWeight, stepWeight, show, options, inputName, signalSelect }) {
        super(id);
        this.name = name;
        this.color = color;
        this.weight = weight;
        this.minWeight = minWeight;
        this.maxWeight = maxWeight;
        this.stepWeight = stepWeight;
        this.show = show;
        this.options = options;
        this.inputName = inputName;
        this.signalSelect = signalSelect;
    }
}

// ✅ **暴露多个类**
module.exports = {
    BaseModel,
    AddressModel,
    ChannelModel
};